package com.ph.shopping.common.core.base;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ph.shopping.common.core.customenum.MessageEnum;
import com.ph.shopping.common.core.customenum.ResultEnum;
import com.ph.shopping.common.util.result.MessageInfo;
import com.ph.shopping.common.util.result.Result;

/**
 * 基础Base
 * 
 * @author WQiang
 * 
 */
public class Base implements ApplicationContextAware {
	// 创建公共日志
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	
	/**
	 * spring上下文对象.
	 */
	protected ApplicationContext applicationContext;
	
	protected MessageInfo getMessageInfo(MessageEnum messageEnum, Object data) {
		MessageInfo info = new MessageInfo();
		info.setCode(messageEnum.getCode());
		info.setMsg(messageEnum.getMsg());
		info.setData(data);
		return info;
	}
	
	protected MessageInfo getMessageInfo(MessageEnum messageEnum) {
		MessageInfo info = new MessageInfo();
		info.setCode(messageEnum.getCode());
		info.setMsg(messageEnum.getMsg());
		return info;
	}

	/**
	 * 
	 * @Title: getResult
	 * @Description: 分页使用
	 * @author WQiang
	 * @date 2017年3月15日 上午11:14:54
	 * @param resultEnum
	 * @param data
	 * @param count
	 * @return
	 */
	protected Result getResult(ResultEnum resultEnum, Object data, long count) {
		Result result = new Result();
		result.setData(data);
		result.setMessage(resultEnum.getMsg());
		result.setCode(resultEnum.getCode());
		result.setCount(count);
		return result;
	}

	/**
	 * 返回数据结果
	 * 
	 * @param resultEnum
	 * @param data
	 * @return
	 */
	protected Result getResult(ResultEnum resultEnum, Object data) {
		Result result = new Result();
		result.setData(data);
		result.setMessage(resultEnum.getMsg());
		result.setCode(resultEnum.getCode());
		return result;
	}

	/**
	 * 只返回信息结果
	 * 
	 * @param resultEnum
	 * @return
	 */
	protected Result getResult(ResultEnum resultEnum) {
		Result result = new Result();
		result.setMessage(resultEnum.getMsg());
		result.setCode(resultEnum.getCode());
		return result;
	}

	/**
	 * json字符串转换成实体
	 * 
	 * @param calzz
	 * @param jsonString
	 * @return
	 */
	protected <T> T unpacket(String jsonString, Class<T> calzz) {
		return new Gson().fromJson(jsonString, calzz);
	}

	/**
	 * json字符串转换成List<T>
	 * 
	 * @param jsonString
	 * @param calzz
	 * @return
	 */
	protected List<?> jsonToList(String jsonString, Class<?> calzz) {
		return new Gson().fromJson(jsonString, new TypeToken<List<?>>() {
		}.getType());
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		// TODO Auto-generated method stub
		this.applicationContext = applicationContext;
	}

	
	/**
	 * 得到Request对象
	 * @author WQiang
	 * @return HttpServletRequest
	 */
	protected HttpServletRequest getRequest() {
		return ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
	}
	
}
